package com.guoxin.part1.chapter3;

import java.util.concurrent.TimeUnit;

/**
 * 捕获中断信号关闭线程
 *
 * @author guoxin
 * @date 2019/8/27
 */
public class InterruptThreadExit2 extends Thread{

    public static void main(String[] args) throws InterruptedException {
        InterruptThreadExit2 t = new InterruptThreadExit2();
        t.start();
        TimeUnit.SECONDS.sleep(2);
        System.out.println("System will be shutdown");
        t.interrupt();
    }

    @Override
    public void run() {
        System.out.println("I will start work");
        for(;;){
            try {
                TimeUnit.MILLISECONDS.sleep(1);
            } catch (InterruptedException e) {
                break;
            }
        }
        System.out.println("I will be exiting.");
    }
}
